//
//  MYTabBarViewController.swift
//  MYWeiBo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

class MYTabBarViewController: UITabBarController {
    
    //定义一个闭包以接收tabbar传送过来加号事件
    let composeCallBack = {() -> () in
        print("传送来的点击事件")
    
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildRootController()
        
        //使用kvc来关连自己tabbar与系统的tabbar
        let myTabBar = MYTabBar()
        myTabBar.composeCallBack = composeCallBack
        setValue(myTabBar, forKey: "tabBar")
        
        
    }
    
    /**
     *添加子控件
    */
    func addChildRootController(){
    
        //创建rootvc
        let homeVC = MYHomeViewController()
        setRootController(homeVC, title: "主页", image: "tabbar_home");
        let messageVC = MYMessageViewController()
        setRootController(messageVC, title: "消息", image: "tabbar_message_center");
        let discoveryVC = MYDiscoveryViewController()
        setRootController(discoveryVC, title: "发现", image: "tabbar_discover");
        let profileVC = MYProfileViewController()
        setRootController(profileVC, title:"我", image: "tabbar_profile");

    }
    
    /**
    *  子控件实现代码
    */
    func setRootController(VC:UIViewController,title:String,image:String){
    
     
        VC.title = title
        VC.tabBarItem.image = UIImage(named: image)
        //设置高亮图片
        
        VC.tabBarItem.selectedImage = UIImage(named:image + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置文字的颜色
      
        VC.tabBarItem.setTitleTextAttributes(  [NSForegroundColorAttributeName:UIColor.orangeColor()], forState:UIControlState.Selected)
        
//        self.addChildViewController(UINavigationController(rootViewController: VC))
        //给控制器包装一下导航控制器
        self.addChildViewController(UINavigationController(rootViewController: VC))
    }

   
}











